package axht.trade.xposed;


import android.view.View;
import android.widget.EditText;

import java.lang.reflect.Field;
import java.security.MessageDigest;

import de.robv.android.xposed.IXposedHookLoadPackage;
import de.robv.android.xposed.XC_MethodHook;
import de.robv.android.xposed.XposedBridge;
import de.robv.android.xposed.XposedHelpers;
import de.robv.android.xposed.callbacks.XC_LoadPackage;

public class Test implements IXposedHookLoadPackage {
    @Override
    public void handleLoadPackage(XC_LoadPackage.LoadPackageParam pparam) throws Throwable {
        // pparam.processName 获取应用程序包名
        // XposedBridge.log("当前启动的应用程序是："+pparam.processName);

        if (pparam.packageName.equals("com.qianyu.zhuceji")) {

            XposedBridge.log("进入包名："+pparam.packageName);

                    //hook 普通方法、静态方法
            XposedHelpers.findAndHookMethod(
                    "com.qianyu.zhuceji.MainActivity", //包名+类名
                    pparam.classLoader,  //类的加载器
                    "checkSN",        //方法名
                    String.class,        //参数列表
                    String.class,
                    new XC_MethodHook() {
                        /**
                         * HOOK之前
                         * 打印参数信息、修改参数
                         * */
                        @Override
                        protected void beforeHookedMethod(MethodHookParam param)
                                throws Throwable {
                            super.beforeHookedMethod(param);
                            XposedBridge.log("修改之前userName：" + param.args[0]);
                            XposedBridge.log("修改之前sn：" + param.args[1]);
                            String userName = (String) param.args[0];
                            MessageDigest digest = MessageDigest.getInstance("MD5");
                            digest.reset();
                            digest.update(userName.getBytes());

                            //主动调用方法
                            String hexstr = (String) XposedHelpers.callStaticMethod(
                                    param.thisObject.getClass(), //获取类
                                    "toHexString",
                                    new Object[]{digest.digest(), ""});

                            StringBuilder sb = new StringBuilder();
                            for (int i = 0; i < hexstr.length(); i += 2) {
                                sb.append(hexstr.charAt(i));
                            }

                            //修改参数
                            //param.args[0]="ke.yijincc.com";
                            //param.args[1]="3192850648@qq.com";
                            param.args[1] = sb.toString();
                            XposedBridge.log("修改之后sn：" + param.args[1]);
                            /**
                             * java反射机制
                             * */
                            // 获取类
                            Class<?> clazz=param.thisObject.getClass();
                            //XposedBridge.log("clazz："+clazz);
                            // 获取字段
                            Field sn=clazz.getDeclaredField("edit_sn");
                            // 设置可见
                            sn.setAccessible(true);// 注意
                            EditText et_sn=(EditText) sn.get(param.thisObject);
                            et_sn.setText(sb.toString());
                        }

                        /**
                         * HOOK之后
                         * 打印返回值信息、修改返回值
                         * */
                        @Override
                        protected void afterHookedMethod(MethodHookParam param)
                                throws Throwable {
                            super.afterHookedMethod(param);
                            XposedBridge.log("返回值：" + param.getResult());
                            //修改返回值之后
                            //param.setResult(true);
                        }
                    });

            //hook 匿名内部类
            XposedHelpers.findAndHookMethod(
                    "com.qianyu.zhuceji.MainActivity$1",
                    pparam.classLoader,
                    "onClick",
                    View.class,
                    new XC_MethodHook() {
                        @Override
                        protected void beforeHookedMethod(MethodHookParam param)
                                throws Throwable {
                            super.beforeHookedMethod(param);
                            View view = (View) param.args[0];
                            XposedBridge.log("view：" + view.getId());
                        }
                    });
        }
    }
}
